package com.zbkj.crmeb.store.service.impl;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.store.dao.StoreZoneDao;
import com.zbkj.crmeb.store.model.StoreZone;
import com.zbkj.crmeb.store.service.HomeStyleService;
import com.zbkj.crmeb.store.service.StoreProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.store.dao.StoreZoneProductDao;
import com.zbkj.crmeb.store.model.StoreZoneProduct;
import com.zbkj.crmeb.store.request.StoreZoneProductRequest;
import com.zbkj.crmeb.store.service.StoreZoneProductService;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Mr.Zhang
* @description StoreZoneProductServiceImpl 接口实现
* @date 2020-05-28 edit by stivepeim 2020-7-4
*/
@Service
public class StoreZoneProductServiceImpl extends ServiceImpl<StoreZoneProductDao, StoreZoneProduct> implements StoreZoneProductService {

    @Resource
    private StoreZoneProductDao dao;
    @Resource
    private StoreZoneDao storeZoneDao;
    @Autowired
    private HomeStyleService homeStyleService;
   @Autowired
    private StoreProductService storeProductService;

    @Autowired
    private IChannelMerchantService channelMerchantService;

    @Override
    public Integer saveStoreZoneProduct(StoreZoneProduct storeZoneProduct) {
        return dao.insert(storeZoneProduct);
    }


    @Override
    public int delete(StoreZoneProductRequest request) {
        StoreZone storeZone = existsStoreZone(request);
        //专区已推送
        if(storeZone.getMerId() == 0 && storeZone.getIsPush() == 1){
            List<StoreZone> storeZoneList = storeZoneDao.selectList(Wrappers.lambdaQuery(StoreZone.class).eq(StoreZone::getIsDel, 0).eq(StoreZone::getPushZoneId, storeZone.getId()));
            List<Integer> zoneIdList = storeZoneList.stream().map(StoreZone::getId).collect(Collectors.toList());
            zoneIdList.add(storeZone.getId());
            return dao.delete(Wrappers.lambdaQuery(StoreZoneProduct.class).eq(StoreZoneProduct::getProductId,request.getProductId()).in(StoreZoneProduct::getZoneId,zoneIdList));
        }else {
            LambdaQueryWrapper<StoreZoneProduct> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(StoreZoneProduct::getProductId, request.getProductId());
            lambdaQueryWrapper.eq(StoreZoneProduct::getZoneId, request.getZoneId());
            return dao.delete(lambdaQueryWrapper);
        }
    }

    public StoreZone existsStoreZone(StoreZoneProductRequest request) {
        StoreZone storeZone = storeZoneDao.selectOne(Wrappers.lambdaQuery(StoreZone.class).eq(StoreZone::getId, request.getZoneId())
                .eq(StoreZone::getMerId, request.getMerId()));
        if (storeZone == null || storeZone.getIsDel() == 1) {
            throw new CrmebException("该专区不存在");
        }
        return storeZone;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(StoreZoneProductRequest request) {
        StoreZone storeZone = existsStoreZone(request);
        if(storeZone.getEndTime() != null && storeZone.getEndTime().compareTo(new Date()) < 0){
            throw new CrmebException("该专区已结束，不能添加商品");
        }
        List<String> productIds = Arrays.asList(request.getProductIds().split(","));
        List<Integer> productIdList = productIds.stream().map(Integer::parseInt).collect(Collectors.toList());
        List<Integer> zoneIdList = new ArrayList<>();
        if(storeZone.getMerId() == 0){
            List<StoreZone> storeZoneList = storeZoneDao.selectList(Wrappers.lambdaQuery(StoreZone.class).eq(StoreZone::getIsDel, 0).eq(StoreZone::getPushZoneId, storeZone.getId()));
            if(CollectionUtils.isNotEmpty(storeZoneList)){
                zoneIdList = storeZoneList.stream().map(StoreZone::getId).collect(Collectors.toList());
            }
        }
        zoneIdList.add(storeZone.getId());
        return addZoneProducts(storeZone.getId(), productIdList, zoneIdList);
    }

    @Override
    public boolean addZoneProducts(Integer id, List<Integer> productIdList, List<Integer> zoneIdList) {
        List<StoreZoneProduct> storeZoneProductList = new ArrayList<>();
        List<StoreZoneProduct> storeZoneProducts = this.list(Wrappers.lambdaQuery(StoreZoneProduct.class).eq(StoreZoneProduct::getZoneId, id));
        List<Integer> existProductIdList = storeZoneProducts.stream().map(StoreZoneProduct::getProductId).collect(Collectors.toList());
        productIdList.removeAll(existProductIdList);
        for (Integer zoneId : zoneIdList) {
            for (Integer productId : productIdList) {
                StoreZoneProduct storeZoneProduct = new StoreZoneProduct();
                storeZoneProduct.setProductId(productId);
                storeZoneProduct.setZoneId(zoneId);
                storeZoneProduct.setCreateTime(new Date());
                storeZoneProductList.add(storeZoneProduct);
            }
        }
        return saveBatch(storeZoneProductList);
    }

    /**
     * 批量移除专区商品
     * @param request
     * @return java.lang.Integer
     */
    @Override
    public Integer delBatchZoneProduct(StoreZoneProductRequest request) {
        if (StringUtils.isBlank(request.getProductIds())){
            throw new CrmebException("商品至少需选择一条");
        }
        StoreZone storeZone = existsStoreZone(request);
        List<String> productIdList = Arrays.asList(request.getProductIds().split(","));
        //专区已推送
        if(storeZone.getMerId() == 0 && storeZone.getIsPush() == 1){
            List<StoreZone> storeZoneList = storeZoneDao.selectList(Wrappers.lambdaQuery(StoreZone.class)
                    .eq(StoreZone::getIsDel, 0)
                    .eq(StoreZone::getPushZoneId, storeZone.getId()));
            List<Integer> zoneIdList = storeZoneList.stream().map(StoreZone::getId).collect(Collectors.toList());
            zoneIdList.add(storeZone.getId());
            return dao.delete(Wrappers.lambdaQuery(StoreZoneProduct.class)
                    .in(StoreZoneProduct::getProductId,productIdList)
                    .in(StoreZoneProduct::getZoneId,zoneIdList));
        }else {
            LambdaQueryWrapper<StoreZoneProduct> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(StoreZoneProduct::getProductId, productIdList);
            lambdaQueryWrapper.eq(StoreZoneProduct::getZoneId, request.getZoneId());
            return dao.delete(lambdaQueryWrapper);
        }
    }

    @Override
    public Boolean update(StoreZoneProductRequest request) {
        LambdaUpdateWrapper<StoreZoneProduct> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StoreZoneProduct::getZoneId, request.getZoneId());
        lambdaUpdateWrapper.eq(StoreZoneProduct::getProductId, request.getProductId());
        lambdaUpdateWrapper.set(StoreZoneProduct::getIsTop, request.getIsTop());
        lambdaUpdateWrapper.set(StoreZoneProduct::getTopTime,request.getIsTop() == 1 ? new Date() : null);
        return update(lambdaUpdateWrapper);

    }
}

